/**
 *  Copyright 2011 Marco Berri - marcoberri@gmail.com
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations under the License.
 **/
package com.mbfasttrack.service;

import com.mbfasttrack.model.Point;
import com.mbfasttrack.utils.GpsUtil;
import java.util.Date;
import java.util.List;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Marco Berri marcoberri@gmail.com http://tecnicume.blogspot.com
 */
@Service("TrackPointService")
@Transactional
public class TrackPointService extends Base {

    /**
     * 
     * @param points
     * @return
     */
    public List<Point> calculateDistance(List<Point> points) {


        Point prec = null;

        for (Point p : points) {

            if (prec == null) {
                prec = p;
                continue;
            }

            Double check_nan = GpsUtil.distance(prec.getLoc().getX(), prec.getLoc().getY(), p.getLoc().getX(), p.getLoc().getY());

            if (check_nan == Double.NaN || check_nan == 0.0 || check_nan < 0.0 || Double.isInfinite(check_nan) || Double.isNaN(check_nan)) {
                p.setDistanceparent(0.0);
                continue;

            }

            p.setDistanceparent(check_nan);
            prec = p;
        }

        return points;
    }

    /**
     * 
     * @param points
     * @return
     */
    public List<Point> calculateTime(List<Point> points) {


        Point prec = null;

        for (Point p : points) {

            if (prec == null) {
                prec = p;
                continue;
            }

            Date d0 = prec.getTime();
            Date d1 = p.getTime();
            double diff = 0.0;
            if (d0 != null && d1 != null) {
                diff = d1.getTime() - d0.getTime();

            }

            p.setTimeparent(diff);

            prec = p;
        }

        return points;
    }
}
